﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Threading;
using System.Diagnostics;
using System.IO;
using System.Text;
using GMUtilitiesDotNet.Services;
using ThouShaltNotBrickLevelEditor.ViewModels;
using ThouShaltNotBrickLevelEditor.Code.Managers;
using GMLogging;

namespace ThouShaltNotBrickLevelEditor
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        #region Constructors
        public App()
        {
            // code from http://stackoverflow.com/questions/520115/stringfomat-localization-problem/520334#520334
            // Ensure the current culture passed into bindings is the OS culture. By default, WPF uses 
            // en-US as the culture, regardless of the system settings.
            FrameworkElement.LanguageProperty.OverrideMetadata(
                typeof(FrameworkElement),
                new FrameworkPropertyMetadata(System.Windows.Markup.XmlLanguage.GetLanguage(System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag)));
            //-----------------------------------------------------------------------------------------

            Startup += AppStartupHandler;
            Exit += AppExitHandler;
            //DispatcherUnhandledException += AppDispatcherUnhandledExceptionHandler;
        }
        #endregion

        #region Event Handlers
        private void AppStartupHandler(object sender, StartupEventArgs e)
        {
            ServiceProvider.RegisterDefaultServices();

            Logger.Initialize("levelEditor.log");

            // create main window view and its view model
            LevelEditorManager levelEditorManager = new LevelEditorManager();

            MainWindow mainWindow = new MainWindow();
            MainWindowViewModel mwvm = new MainWindowViewModel(levelEditorManager);
            mainWindow.DataContext = mwvm;

            mainWindow.Show();
        }

        private void AppExitHandler(object sender, ExitEventArgs e)
        {
            Logger.Destroy();
        }

        //private void AppDispatcherUnhandledExceptionHandler(object sender, DispatcherUnhandledExceptionEventArgs e)
        //{
        //    e.Handled = true;

        //    StringBuilder sb = new StringBuilder();
        //    sb
        //        .Append("Exception:")
        //        .Append(Environment.NewLine)
        //        .Append(e.Exception.Message)
        //        .Append(Environment.NewLine)
        //        .Append(Environment.NewLine)
        //        .Append("Stack Trace:")
        //        .Append(Environment.NewLine)
        //        .Append(e.Exception.StackTrace);
        //    MessageBox.Show(sb.ToString(), "Unhandled exception", MessageBoxButton.OK, MessageBoxImage.Error);

        //    Application.Current.Shutdown();
        //}
        #endregion
    }
}
